package com.gxa.modules.service.accessservice.impl;

import com.gxa.common.R;
import com.gxa.modules.mapper.accessmapper.AlarmRulesMapper;
import com.gxa.modules.pojo.AlarmRules;
import com.gxa.modules.pojo.AlarmType;
import com.gxa.modules.pojo.PushMode;
import com.gxa.modules.pojo.Role;
import com.gxa.modules.service.accessservice.AlarmRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;

@Service
public class AlarmRulesServiceImpl implements AlarmRulesService {

    @Autowired
    private AlarmRulesMapper alarmRulesMapper;

    public R selecAalarmRules(String alarmRulesName, String pushModeId,String page,String size){

        R<Object> r = new R<>();

        int statrIndex=(Integer.parseInt(page)-1)*Integer.parseInt(size);
        int endIndex=Integer.parseInt(page)*Integer.parseInt(size);

        r.setCode(200);
        r.setMsg("查询成功！");
        r.setData(alarmRulesMapper.selecAalarmRules(alarmRulesName,pushModeId,statrIndex,endIndex));
        return r;
    }

    @Override
    public R insertAalarmRules(AlarmRules alarmRules) {
        R<Object> r = new R<>();

        r.setCode(500);
        r.setMsg("新增失败！");

        if (alarmRulesMapper.insertAalarmRules(alarmRules)>0){
            r.setCode(200);
            r.setMsg("新增成功！");
        }
        return r;
    }

    @Override
    public R deleteAalarmRules(String alarmRulesId) {
        R<Object> r = new R<>();

        if ( alarmRulesId ==null || "".equals(alarmRulesId) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        String[] strings = alarmRulesId.split(",");
        for (int i=0;i<strings.length;i++) {
            alarmRulesId=strings[i];
            if(alarmRulesMapper.deleteAalarmRules(alarmRulesId)>0){
                r.setCode(200);
                r.setMsg("删除成功！");
            }
        }

        return r;
    }

    @Override
    public R updateAalarmRules(AlarmRules alarmRules) {
        R<Object> r = new R<>();

        r.setCode(500);
        r.setMsg("修改失败！");

        if (alarmRulesMapper.updateAalarmRules(alarmRules)>0){
            r.setCode(200);
            r.setMsg("修改成功！");
        }
        return r;
    }
}
